package com.sky.orangehireserver.modules.admin.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.sky.orangehireserver.aspect.ApiOperationLog;
import com.sky.orangehireserver.modules.admin.dto.OrganizationCreateDTO;
import com.sky.orangehireserver.modules.admin.dto.OrganizationQueryDTO;
import com.sky.orangehireserver.modules.admin.dto.OrganizationUpdateDTO;
import com.sky.orangehireserver.modules.admin.service.AdminEnterpriseService;
import com.sky.orangehireserver.modules.admin.vo.OrganizationVO;
import com.sky.orangehireserver.utils.Response;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 超级管理员企业管理控制器
 * 
 * @author sky
 */
@Slf4j
@RestController
@RequestMapping("/admin/organizations")
@RequiredArgsConstructor
@Tag(name = "超级管理员端-企业管理", description = "超级管理员端-企业管理相关接口")
public class AdminEnterpriseController {

    private final AdminEnterpriseService adminEnterpriseService;

    /**
     * 创建企业
     * 
     * @param createDTO 企业创建请求DTO
     * @return 企业信息VO
     */
    @PostMapping
    @Operation(summary = "创建企业", description = "超级管理员创建新企业")
    @ApiOperationLog(description = "创建企业")
    public Response<OrganizationVO> createOrganization(@Valid @RequestBody OrganizationCreateDTO createDTO) {
        OrganizationVO organizationVO = adminEnterpriseService.createOrganization(createDTO);
        return Response.success(organizationVO);
    }

    /**
     * 更新企业信息
     * 
     * @param updateDTO 企业更新请求DTO
     * @return 企业信息VO
     */
    @PutMapping
    @Operation(summary = "更新企业信息", description = "超级管理员更新企业信息")
    @ApiOperationLog(description = "更新企业信息")
    public Response<OrganizationVO> updateOrganization(@Valid @RequestBody OrganizationUpdateDTO updateDTO) {
        OrganizationVO organizationVO = adminEnterpriseService.updateOrganization(updateDTO);
        return Response.success(organizationVO);
    }

    /**
     * 删除企业
     * 
     * @param id 企业ID
     * @return 操作结果
     */
    @DeleteMapping("/{id}")
    @Operation(summary = "删除企业", description = "超级管理员删除企业")
    @ApiOperationLog(description = "删除企业")
    public Response<Void> deleteOrganization(@Parameter(description = "企业ID") @PathVariable Long id) {
        adminEnterpriseService.deleteOrganization(id);
        return Response.success();
    }

    /**
     * 获取企业详情
     * 
     * @param id 企业ID
     * @return 企业信息VO
     */
    @GetMapping("/{id}")
    @Operation(summary = "获取企业详情", description = "根据ID获取企业详细信息")
    @ApiOperationLog(description = "获取企业详情")
    public Response<OrganizationVO> getOrganizationById(@Parameter(description = "企业ID") @PathVariable Long id) {
        OrganizationVO organizationVO = adminEnterpriseService.getOrganizationById(id);
        return Response.success(organizationVO);
    }

    /**
     * 分页查询企业列表
     * 
     * @param queryDTO 查询条件DTO
     * @return 分页企业信息
     */
    @GetMapping("/page")
    @Operation(summary = "分页查询企业列表", description = "根据条件分页查询企业列表")
    @ApiOperationLog(description = "分页查询企业列表")
    public Response<IPage<OrganizationVO>> getOrganizationPage(@Valid OrganizationQueryDTO queryDTO) {
        IPage<OrganizationVO> organizationPage = adminEnterpriseService.getOrganizationPage(queryDTO);
        return Response.success(organizationPage);
    }

    /**
     * 获取所有企业列表
     * 
     * @return 企业信息列表
     */
    @GetMapping("/all")
    @Operation(summary = "获取所有企业列表", description = "获取所有企业列表（不分页）")
    @ApiOperationLog(description = "获取所有企业列表")
    public Response<List<OrganizationVO>> getAllOrganizations() {
        List<OrganizationVO> organizations = adminEnterpriseService.getAllOrganizations();
        return Response.success(organizations);
    }

    /**
     * 启用企业
     * 
     * @param id 企业ID
     * @return 操作结果
     */
    @PutMapping("/{id}/enable")
    @Operation(summary = "启用企业", description = "启用指定企业")
    @ApiOperationLog(description = "启用企业")
    public Response<Void> enableOrganization(@Parameter(description = "企业ID") @PathVariable Long id) {
        adminEnterpriseService.enableOrganization(id);
        return Response.success();
    }

    /**
     * 禁用企业
     * 
     * @param id 企业ID
     * @return 操作结果
     */
    @PutMapping("/{id}/disable")
    @Operation(summary = "禁用企业", description = "禁用指定企业")
    @ApiOperationLog(description = "禁用企业")
    public Response<Void> disableOrganization(@Parameter(description = "企业ID") @PathVariable Long id) {
        adminEnterpriseService.disableOrganization(id);
        return Response.success();
    }

    /**
     * 批量删除企业
     * 
     * @param ids 企业ID列表
     * @return 操作结果
     */
    @DeleteMapping("/batch")
    @Operation(summary = "批量删除企业", description = "批量删除多个企业")
    @ApiOperationLog(description = "批量删除企业")
    public Response<Void> batchDeleteOrganizations(@Parameter(description = "企业ID列表") @RequestBody List<Long> ids) {
        adminEnterpriseService.batchDeleteOrganizations(ids);
        return Response.success();
    }
}
